package com.neu.view.impl;

import com.neu.dao.ClientDao;
import com.neu.dao.impl.ClientDaoImpl;
import com.neu.util.PrintMessage;
import com.neu.util.ScaUtil;
import com.neu.view.UserView;

import java.util.List;
import java.util.Map;

public class ClientView extends UserView {

    ClientDao clientDao = new ClientDaoImpl();

    /**
     * 登录方法
     * @param name 用户名
     * @param pwd  密码
     * @return
     */
    @Override
    public void login(String name, String pwd) {
        Map<String,Object> map = clientDao.login(name,pwd);
        if(map == null || map.isEmpty()){
            System.out.println("用户名或密码错误,请重新输入");
        }else {
            this.setMap(map);
            System.out.println("欢迎"+map.get("client_name"));
            this.menu();
        }
    }

    /**
     * 注册方法
     * @param name 用户名
     * @param pwd  用户密码
     * @param sex  性别 0女,男1
     * @param birthday 生日
     * @param client_tel 联系电话
     * @return
     */
    @Override
    public void register(String name, String sex, String birthday, String pwd, String client_tel) {
        int row = clientDao.register(name,sex,birthday,pwd,client_tel);
        if(row == 0){
            System.out.println("注册失败,请重新注册");
        }else {
            this.setMap(clientDao.login(name,pwd));
            System.out.println("欢迎"+this.getMap().get("client_name"));
            this.menu();
        }
    }

    /**
     * 菜单
     */
    @Override
    public void menu() {
        while(true){
            System.out.println("1:查询用户信息");
            System.out.println("2:修改用户信息");
            System.out.println("4:进入账本");
            System.out.println("5:退出系统");
            String sr = ScaUtil.getScanner().next();
            switch (sr){
                case "1":
                    //查询用户信息
                    searchClient();
                    break;
                case "2":
                    //修改用户信息
                    updateClientMeg();
                    break;
                case "3":
                    //修改账户密码
                    updateClientPwd();
                    break;
                case "4":
                    //进入账本
                    UserView userView = new TradeView();
                    userView.setMap(getMap());
                    userView.menu();
                    break;
                case "5":
                    System.exit(0);
                    break;
                default:
                    System.out.println("请合法输入");
                    break;
            }
        }
    }

    /**
     * 修改用户信息
     */
    private void updateClientMeg() {
        clientDao.searchClient(getMap().get("client_no").toString());
        System.out.println("继续修改输入:1");
        String sr = ScaUtil.getScanner().next();
        if( !"1".equals(sr) ){
            return;
        }
        System.out.println("输入用户名");
        String client_name = ScaUtil.getScanner().next();
        System.out.println("输入出生年月");
        String birthday = ScaUtil.getScanner().next();
        int row = clientDao.updateClinetMsg(getMap().get("client_no").toString(),client_name,birthday);
        PrintMessage.printNum(row);
    }

    /**
     * 修改用户密码
     */
    private void updateClientPwd() {
        String oldPwd = null;
        String newPwd = null;
        while(true){
            System.out.println("请输入旧密码");
            oldPwd = ScaUtil.getScanner().next();
            System.out.println("请输入新密码");
            newPwd = ScaUtil.getScanner().next();
            if( oldPwd.equals(newPwd) ){
                System.out.println("新密码与旧密码相同,请重新输入");
                continue;
            }
            while(true){
                System.out.println("确认密码:");
                String newPwd1 = ScaUtil.getScanner().next();
                if( newPwd.equals(newPwd1) ){
                    System.out.println("重新输入");
                }else {
                    break;
                }
            }
            break;
        }
        int row = clientDao.updateClientPwd(getMap().get("client_no").toString(),oldPwd,newPwd);
        PrintMessage.printNum(row);
    }

    /**
     * 查询当前用户信息
     */
    private void searchClient() {
        Map<String,Object> map = null;
        map = clientDao.searchClient(getMap().get("client_no").toString());
        PrintMessage.printClientMap(map);
    }

}
